//------------------------------------------------------------------------------
#include <QMessageBox>
#include "CComboResultFrm.h"
#include "CSelectTypeBLFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CSelectTypeBLFrm::CSelectTypeBLFrm(QWidget *parent) : QDialog(parent) {
	setupUi(this);
	devisSelected=false;
	commandeSelected=false;
}
//------------------------------------------------------------------------------
void CSelectTypeBLFrm::onComboDevis(CMyComboBox *comboBox) {
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this, 1);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted) {
		infoDevis=QMap<QString, QString>(comboResultFrm->getResults());
		comboBox->setValue(infoDevis.value("teteDevis.nomDevis"));
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CSelectTypeBLFrm::onComboCommande(CMyComboBox *comboBox) {
	CComboResultFrm *comboResultFrm=new CComboResultFrm(comboBox->getTableRemp(), comboBox->getChampRemp(), this);
	
	comboResultFrm->setModal(true);
	comboResultFrm->exec();
	
	if(comboResultFrm->result() == QDialog::Accepted) {
		infoCommande=QMap<QString, QString>(comboResultFrm->getResults());
		comboBox->setValue(infoCommande.value("teteCommandes.nomCommande"));
	}
	
	delete comboResultFrm;
}
//------------------------------------------------------------------------------
void CSelectTypeBLFrm::on_optionSimple_clicked(bool checked)
{
	devis->setEnabled(false);
	devisSelected=false;
	commande->setEnabled(false);
	commandeSelected=false;
}
//------------------------------------------------------------------------------
void CSelectTypeBLFrm::on_optionDevis_clicked(bool checked)
{
	devis->setEnabled(true);
	devisSelected=true;
	commande->setEnabled(false);
	commandeSelected=false;
}
//------------------------------------------------------------------------------
void CSelectTypeBLFrm::on_optionCommande_clicked(bool checked)
{
	devis->setEnabled(false);
	devisSelected=false;
	commande->setEnabled(true);
	commandeSelected=true;
}
//------------------------------------------------------------------------------
void CSelectTypeBLFrm::accept(void)
{
	if(optionDevis->isChecked())
	{
		if(!CMySQL::getInstance()->existe("teteDevis", "nomDevis", devis->getValue()))
		{
			QMessageBox msgBox(	QMessageBox::Warning, QObject::tr("Erreur"), 
								tr("Le devis '")+devis->getValue()+tr("' n'existe pas, impossible de créer la facture"), QMessageBox::Ok);
			msgBox.exec();	
			
			return;
		}
	}
		
	if(optionCommande->isChecked())
	{
		if(!CMySQL::getInstance()->existe("teteCommandes", "nomCommande", commande->getValue()))
		{
			QMessageBox msgBox(	QMessageBox::Warning, QObject::tr("Erreur"), 
								tr("La commande '")+commande->getValue()+tr("' n'existe pas, impossible de créer la facture"), QMessageBox::Ok);
			msgBox.exec();
			
			return;
		}
	}
	
	QDialog::accept();
}
//------------------------------------------------------------------------------